﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace WMM.Tapioca.Formula.Aspects
{
    /// <summary>
    /// A period rule is an aspect rule that addresses the period aspect and that
    /// is expressed by the <formula:period> element.
    /// </summary>
    public class PeriodAspect : Aspect
    {
        private XElement element;

        internal XElement Element
        {
            get { return element; }
            set { element = value; }
        }
        
        private string forever;

        /// <returns> If the period rule has a <formula:forever> child element 
        /// then the RAV is a forever period. </returns>
        internal string Forever
        {
            get { return forever; }
            set { forever = value; }
        }

        private string instant;

        /// <returns> If the period rule has a <formula:instant> child element then the RAV has 
        /// an instant with a value that is equal to that obtained by evaluating the XPath expression 
        /// contained by the @value attribute on the <formula:instant> element.  </returns>
        internal string Instant
        {
            get { return instant; }
            set { instant = value; }
        }

        private string durationStart;

        /// <returns> If the period rule has a <formula:duration> child element then the RAV has a finite duration
        /// with a start value that is equal to that obtained by evaluating the XPath expression contained by 
        /// the @start attribute on the <formula:duration> element and an end value that is equal to that obtained 
        /// by evaluating the XPath expression contained by the @end attribute on the <formula:duration> element.  </returns>
        internal string DurationStart
        {
            get { return durationStart; }
            set { durationStart = value; }
        }

        private string durationEnd;

        internal string DurationEnd
        {
            get { return durationEnd; }
            set { durationEnd = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public PeriodAspect(XElement periodAspect)
        {
            this.Element = periodAspect;

            XElement aspect = periodAspect.Parent;

            if (aspect.Attribute("source") != null)
            {
                this.Source = aspect.Attribute("source").Value;
            }

            if (periodAspect.Attribute("source") != null)
            {
                this.Source = periodAspect.Attribute("source").Value;
            }

            XElement forever = periodAspect.Element(formula + "forever");

            if (forever != null)
            {
                this.Forever = "forever";
            }

            XElement instant = periodAspect.Element(formula + "instant");

            if (instant != null)
            {
                XAttribute value = instant.Attribute("value");

                if (value != null)
                {
                    this.Instant = value.Value;
                }
            }

            XElement duration = periodAspect.Element(formula + "duration");

            if (duration != null)
            {
                XAttribute start = duration.Attribute("start");

                if (start != null)
                {
                    this.DurationStart = start.Value;
                }

                XAttribute end = duration.Attribute("end");

                if (end != null)
                {
                    this.DurationEnd = end.Value;
                }
            }
        }
    }
}
